package com.gd.iot.energy.service.statistics.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gd.iot.energy.code.ResponseCode;
import com.gd.iot.energy.consts.SystemConsts;
import com.gd.iot.energy.dao.statistics.ReportHistorydataPOMapper;
import com.gd.iot.energy.exception.MyException;
import com.gd.iot.energy.po.systemadmin.AreaArchivesPO;
import com.gd.iot.energy.pojo.common.vo.ColumnVO;
import com.gd.iot.energy.pojo.common.vo.PageVO;
import com.gd.iot.energy.pojo.common.vo.ChartVO;
import com.gd.iot.energy.pojo.statistics.dto.*;
import com.gd.iot.energy.pojo.statistics.vo.*;
import com.gd.iot.energy.service.dailymanage.CustomerFeeService;
import com.gd.iot.energy.service.statistics.StatisticsService;
import com.gd.iot.energy.service.systemadmin.AreaOrganizationService;
import com.gd.iot.energy.util.CustomColumnUtil;
import com.gd.iot.energy.util.CustomDateUtil;
import com.gd.iot.energy.util.CustomStrUtil;
import com.gd.iot.energy.util.EnergyTypeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 数据统计
 */
@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private AreaOrganizationService areaOrganizationService;

    @Autowired
    private CustomerFeeService customerFeeService;

    @Resource
    private ReportHistorydataPOMapper reportHistorydataPOMapper;

    /**
     * 同比数据表头配置文件
     */
    private static final String YEAR_TO_YEAR_LIST_COLUMN_FILE_NAME = "statistics/year_to_year_list.column";

    /**
     * 环比数据表头配置文件
     */
    private static final String LOOP_RATIO_LIST_COLUMN_FILE_NAME = "statistics/loop_ratio_list.column";

    /**
     * 用户用量表头配置文件
     */
    private static final String USER_DOSAGE_LIST_COLUMN_FILE_NAME = "statistics/user_dosage_list.column";

    /**
     * 零用量表头配置文件
     */
    private static final String ZERO_DOSAGE_LIST_COLUMN_FILE_NAME = "statistics/zero_dosage_list.column";

    /**
     * 突增突减表头配置文件
     */
    private static final String JUMP_AND_ANTICLIMAX_LIST_COLUMN_FILE_NAME = "statistics/jump_and_anticlimax.column";


    @Override
    public PageVO<StatisticsPageVO> getYearOnYearStatisticsPageVO(String customerSerialnumber,
                                                                  String customerName, String customerType,
                                                                  String cycle, String date, Integer areaId,
                                                                  Integer pageSize, Integer pageNum,
                                                                  String orderColumn, String orderType,
                                                                  SystemConsts.StatisticsType statisticsType) {

        if (StrUtil.isBlank(cycle) || StrUtil.isBlank(date)) {
            throw new MyException(ResponseCode.PARAMETER_REQUIRED);
        }

        List<AreaArchivesPO> areaOrganizationPOS = customerFeeService.selectByParams(customerSerialnumber, customerName,
                customerType, areaId, null);
        List<Long> archiveIds = areaOrganizationPOS.stream().map(AreaArchivesPO::getId)
                .collect(Collectors.toList());

        IPage<StatisticsDTO> page = new Page<>(pageNum, pageSize);

        QueryWrapper<StatisticsDTO> queryWrapperOrder = new QueryWrapper<>();

        //为防止档案为空，放一个不存在的-999999查询条件
        archiveIds = archiveIds.isEmpty() ? Arrays.asList(-999999L) : archiveIds;
        if (StrUtil.isNotBlank(orderColumn) && StrUtil.isNotBlank(orderType)) {
            orderColumn = CustomStrUtil.humpToLine(orderColumn);
            queryWrapperOrder.orderBy(true, orderType.equals(SystemConsts.OrderType.ASC.value), orderColumn);
        }

        Map<String, String> beginAndEndDate = CustomDateUtil.getBeginAndEndDate(SystemConsts.DATE_CYCLE_TYPE.getEnumByValue(cycle), date, statisticsType);
        QueryWrapper<StatisticsDTO> queryWrapper = getStatisticsDTOQueryWrapper(archiveIds);

        IPage<StatisticsDTO> selectHistoryPage = reportHistorydataPOMapper.selectYearToYearPage(page,
                queryWrapper, queryWrapperOrder, beginAndEndDate.get("beginDate"), beginAndEndDate.get("endDate"),
                beginAndEndDate.get("lastBeginDate"), beginAndEndDate.get("lastEndDate"), archiveIds);
        PageVO<StatisticsPageVO> pageVO = new PageVO<>();
        pageVO.setPageSize(pageSize);
        pageVO.setPageNum(pageNum);
        List<StatisticsPageVO> statisticsPageVOList = convertToStatisticsPageVO(selectHistoryPage.getRecords(), beginAndEndDate);
        pageVO.setData(statisticsPageVOList);
        pageVO.setTotal(selectHistoryPage.getTotal());
        pageVO.setPages(selectHistoryPage.getPages());

        //表头
        List<ColumnVO> columns = CustomColumnUtil.getColumn(statisticsType.equals(SystemConsts.StatisticsType.YEAR_TO_YEAR)
                ? YEAR_TO_YEAR_LIST_COLUMN_FILE_NAME : LOOP_RATIO_LIST_COLUMN_FILE_NAME);
        pageVO.setColumns(columns);
        return pageVO;
    }

    @Override
    public List<ChartVO> getStatisticsChartVO(String customerSerialnumber, String customerName, String customerType, String cycle,
                                              String date, Integer areaId, SystemConsts.StatisticsType statisticsType) {
        if (StrUtil.isBlank(cycle) || StrUtil.isBlank(date)) {
            throw new MyException(ResponseCode.PARAMETER_REQUIRED);
        }

        List<AreaArchivesPO> areaOrganizationPOS = customerFeeService.selectByParams(customerSerialnumber, customerName,
                customerType, areaId, null);
        List<Long> archiveIds = areaOrganizationPOS.stream().map(AreaArchivesPO::getId)
                .collect(Collectors.toList());


        //为防止档案为空，放一个不存在的-999999查询条件
        archiveIds = archiveIds.isEmpty() ? Arrays.asList(-999999L) : archiveIds;


        Map<String, String> beginAndEndDate = CustomDateUtil.getBeginAndEndDate(SystemConsts.DATE_CYCLE_TYPE.getEnumByValue(cycle), date, statisticsType);
        QueryWrapper<StatisticsDTO> queryWrapper = getStatisticsDTOQueryWrapper(archiveIds);

        StatisticsDTO statisticsDTO = reportHistorydataPOMapper.selectYearToYearChart(
                queryWrapper, beginAndEndDate.get("beginDate"), beginAndEndDate.get("endDate"),
                beginAndEndDate.get("lastBeginDate"), beginAndEndDate.get("lastEndDate"), archiveIds);
        if(statisticsDTO==null){
            return new ArrayList<>(0);
        }
        List<ChartVO> chartVOList = convertToStatisticsChartVO(statisticsDTO, beginAndEndDate);

        return chartVOList;
    }

    @Override
    public List<ChartVO> getTrendStatisticsChartVO(String beginDate, String endDate, Integer areaId) {
        if (StrUtil.isBlank(beginDate) || StrUtil.isBlank(endDate)) {
            throw new MyException(ResponseCode.PARAMETER_REQUIRED);
        }

        List<AreaArchivesPO> areaOrganizationPOS = customerFeeService.selectByParams(null, null,
                null, areaId, null);
        List<Long> archiveIds = areaOrganizationPOS.stream().map(AreaArchivesPO::getId)
                .collect(Collectors.toList());


        //为防止档案为空，放一个不存在的-999999查询条件
        archiveIds = archiveIds.isEmpty() ? Arrays.asList(-999999L) : archiveIds;

        QueryWrapper<StatisticsDTO> queryWrapper = getStatisticsDTOQueryWrapper(archiveIds);
        queryWrapper.between("read_meter_time", DateUtil.beginOfDay(DateUtil.parseDate(beginDate)), DateUtil.endOfDay(DateUtil.parseDate(endDate)));
        List<StatisticsDTO> statisticsDTOList = reportHistorydataPOMapper.selectTrendChart(queryWrapper);
        List<String> datesBetweenTwoDate = CustomDateUtil.getDatesBetweenTwoDate(beginDate, endDate);
        List<ChartVO> chartVOList = convertToTrendChartVO(statisticsDTOList, datesBetweenTwoDate);

        return chartVOList;
    }

    @Override
    public PageVO<UserDosagePageVO> getUserDosagePageVO(String customerSerialnumber, String customerName, String archivesAddress, String cycle, String date, Integer areaId, Integer pageSize, Integer pageNum) {
        if (StrUtil.isBlank(cycle) || StrUtil.isBlank(date)) {
            throw new MyException(ResponseCode.PARAMETER_REQUIRED);
        }

        List<AreaArchivesPO> areaOrganizationPOS = customerFeeService.selectByParams(customerSerialnumber, customerName, null, areaId, archivesAddress);
        List<Long> archiveIds = areaOrganizationPOS.stream().map(AreaArchivesPO::getId)
                .collect(Collectors.toList());

        IPage<StatisticsDTO> page = new Page<>(pageNum, pageSize);

        //为防止档案为空，放一个不存在的-999999查询条件
        archiveIds = archiveIds.isEmpty() ? Arrays.asList(-999999L) : archiveIds;

        Map<String, String> beginAndEndDate = CustomDateUtil.getBeginAndEndDate(SystemConsts.DATE_CYCLE_TYPE.getEnumByValue(cycle),
                date, SystemConsts.StatisticsType.YEAR_TO_YEAR);
        QueryWrapper<StatisticsDTO> queryWrapper = getStatisticsDTOQueryWrapper(archiveIds);
        queryWrapper.between("freeze_time", beginAndEndDate.get("begin"), beginAndEndDate.get("end"));

        IPage<UserDosagePageDTO> userDosagePageDTOPage = reportHistorydataPOMapper.selectUserDosagePage(page,
                queryWrapper);
        PageVO<UserDosagePageVO> pageVO = new PageVO<>();
        pageVO.setPageSize(pageSize);
        pageVO.setPageNum(pageNum);
        List<UserDosagePageVO> userDosagePageVOS = convertToUserDosagePageVO(userDosagePageDTOPage.getRecords(), cycle, beginAndEndDate);
        pageVO.setData(userDosagePageVOS);
        pageVO.setTotal(userDosagePageDTOPage.getTotal());
        pageVO.setPages(userDosagePageDTOPage.getPages());

        //表头
        List<ColumnVO> columns = CustomColumnUtil.getColumn(USER_DOSAGE_LIST_COLUMN_FILE_NAME);
        pageVO.setColumns(columns);
        return pageVO;
    }

    @Override
    public List<UserDosageChartVO> getUserDosageChartVO(String customerSerialnumber, String customerName, String archivesAddress, String cycle, String date, Integer areaId) {
        if (StrUtil.isBlank(cycle) || StrUtil.isBlank(date)) {
            throw new MyException(ResponseCode.PARAMETER_REQUIRED);
        }

        List<AreaArchivesPO> areaOrganizationPOS = customerFeeService.selectByParams(customerSerialnumber, customerName, null, areaId, archivesAddress);
        List<Long> archiveIds = areaOrganizationPOS.stream().map(AreaArchivesPO::getId)
                .collect(Collectors.toList());

        //为防止档案为空，放一个不存在的-999999查询条件
        archiveIds = archiveIds.isEmpty() ? Arrays.asList(-999999L) : archiveIds;

        Map<String, String> beginAndEndDate = CustomDateUtil.getBeginAndEndDate(SystemConsts.DATE_CYCLE_TYPE.getEnumByValue(cycle),
                date, SystemConsts.StatisticsType.YEAR_TO_YEAR);
        QueryWrapper<StatisticsDTO> queryWrapper = getStatisticsDTOQueryWrapper(archiveIds);
        queryWrapper.between("freeze_time", beginAndEndDate.get("begin"), beginAndEndDate.get("end"));

        List<UserDosageChartDTO> userDosageChartDTOS = reportHistorydataPOMapper.selectUserDosageChart(queryWrapper);
        List<UserDosageChartVO> userDosageChartVOS = convertToUserDosageChartVO(userDosageChartDTOS);

        return userDosageChartVOS;
    }

    @Override
    public PageVO<ZeroDosagePageVO> getZeroDosagePageVO(String customerSerialnumber, String customerName, String archivesAddress, String cycle, String date, Integer areaId, Integer pageSize, Integer pageNum) {
        if (StrUtil.isBlank(cycle) || StrUtil.isBlank(date)) {
            throw new MyException(ResponseCode.PARAMETER_REQUIRED);
        }

        List<AreaArchivesPO> areaOrganizationPOS = customerFeeService.selectByParams(customerSerialnumber, customerName, null, areaId, archivesAddress);
        List<Long> archiveIds = areaOrganizationPOS.stream().map(AreaArchivesPO::getId)
                .collect(Collectors.toList());

        IPage<StatisticsDTO> page = new Page<>(pageNum, pageSize);

        //为防止档案为空，放一个不存在的-999999查询条件
        archiveIds = archiveIds.isEmpty() ? Arrays.asList(-999999L) : archiveIds;

        Map<String, String> beginAndEndDate = CustomDateUtil.getBeginAndEndDate(SystemConsts.DATE_CYCLE_TYPE.getEnumByValue(cycle),
                date, SystemConsts.StatisticsType.YEAR_TO_YEAR);
        QueryWrapper<StatisticsDTO> queryWrapper = getStatisticsDTOQueryWrapper(archiveIds);
        queryWrapper.between("freeze_time", beginAndEndDate.get("begin"), beginAndEndDate.get("end"));

        IPage<ZeroDosagePageDTO> zeroDosagePage = reportHistorydataPOMapper.selectZeroDosagePage(page,
                queryWrapper);
        PageVO<ZeroDosagePageVO> pageVO = new PageVO<>();
        pageVO.setPageSize(pageSize);
        pageVO.setPageNum(pageNum);
        List<ZeroDosagePageVO> userDosagePageVOS = convertToZeroDosagePageVO(zeroDosagePage.getRecords(), cycle, beginAndEndDate);
        pageVO.setData(userDosagePageVOS);
        pageVO.setTotal(zeroDosagePage.getTotal());
        pageVO.setPages(zeroDosagePage.getPages());

        //表头
        List<ColumnVO> columns = CustomColumnUtil.getColumn(ZERO_DOSAGE_LIST_COLUMN_FILE_NAME);
        pageVO.setColumns(columns);
        return pageVO;
    }

    @Override
    public PageVO<JumpAndAnticlimaxPageVO> getJumpAndAnticlimaxPageVO(String differenceType, String differenceValue,
                                                                      String cycle, String startDate,
                                                                      String endDate, Integer areaId,
                                                                      Integer pageSize,
                                                                      Integer pageNum) {
        if (StrUtil.isBlank(cycle)
                || StrUtil.isBlank(startDate)
                || StrUtil.isBlank(endDate)
                || StrUtil.isBlank(differenceType)
                || StrUtil.isBlank(differenceValue)) {
            throw new MyException(ResponseCode.PARAMETER_REQUIRED);
        }
        if (SystemConsts.DifferenceType.getValueNameByValue(differenceType) == null
                || SystemConsts.DATE_CYCLE_TYPE.getEnumByValue(cycle) == null) {
            throw new MyException(ResponseCode.PARAMETER_ERROR);
        }

        List<AreaArchivesPO> areaOrganizationPOS = customerFeeService.selectByParams(null, null, null, areaId, null);
        List<Long> archiveIds = areaOrganizationPOS.stream().map(AreaArchivesPO::getId)
                .collect(Collectors.toList());

        IPage<StatisticsDTO> page = new Page<>(pageNum, pageSize);

        //为防止档案为空，放一个不存在的-999999查询条件
        archiveIds = archiveIds.isEmpty() ? Arrays.asList(-999999L) : archiveIds;

        Map<String, String> beginAndEndDate = CustomDateUtil.getStartAndEndTimeByType(SystemConsts.DATE_CYCLE_TYPE.getEnumByValue(cycle),
                startDate, endDate);
        QueryWrapper<StatisticsDTO> queryWrapper = getStatisticsDTOQueryWrapper(archiveIds);
        queryWrapper.between("freeze_time", beginAndEndDate.get("beginDate"), beginAndEndDate.get("endDate"));

        IPage<JumpAndAnticlimaxPageDTO> jumpAndAnticlimaxPage = reportHistorydataPOMapper.selectJumpAndAnticlimaxPage(page,
                queryWrapper, differenceType, differenceValue);
        PageVO<JumpAndAnticlimaxPageVO> pageVO = new PageVO<>();
        pageVO.setPageSize(pageSize);
        pageVO.setPageNum(pageNum);
        List<JumpAndAnticlimaxPageVO> jumpAndAnticlimaxPageVOS = convertToJumpAndAnticlimaxPageVO(jumpAndAnticlimaxPage.getRecords(), cycle);
        pageVO.setData(jumpAndAnticlimaxPageVOS);
        pageVO.setTotal(jumpAndAnticlimaxPage.getTotal());
        pageVO.setPages(jumpAndAnticlimaxPage.getPages());

        //表头
        List<ColumnVO> columns = CustomColumnUtil.getColumn(JUMP_AND_ANTICLIMAX_LIST_COLUMN_FILE_NAME);
        pageVO.setColumns(columns);
        return pageVO;
    }

    @Override
    public List<JumpAndAnticlimaxChartVO> getJumpAndAnticlimaxChartVO(String differenceType, String differenceValue, String cycle, String startDate, String endDate, Integer areaId) {
        if (StrUtil.isBlank(cycle)
                || StrUtil.isBlank(startDate)
                || StrUtil.isBlank(endDate)
                || StrUtil.isBlank(differenceType)
                || StrUtil.isBlank(differenceValue)) {
            throw new MyException(ResponseCode.PARAMETER_REQUIRED);
        }
        if (SystemConsts.DifferenceType.getValueNameByValue(differenceType) == null
                || SystemConsts.DATE_CYCLE_TYPE.getEnumByValue(cycle) == null) {
            throw new MyException(ResponseCode.PARAMETER_ERROR);
        }

        List<AreaArchivesPO> areaOrganizationPOS = customerFeeService.selectByParams(null, null, null, areaId, null);
        List<Long> archiveIds = areaOrganizationPOS.stream().map(AreaArchivesPO::getId)
                .collect(Collectors.toList());


        //为防止档案为空，放一个不存在的-999999查询条件
        archiveIds = archiveIds.isEmpty() ? Arrays.asList(-999999L) : archiveIds;

        Map<String, String> beginAndEndDate = CustomDateUtil.getStartAndEndTimeByType(SystemConsts.DATE_CYCLE_TYPE.getEnumByValue(cycle),
                startDate, endDate);
        QueryWrapper<StatisticsDTO> queryWrapper = getStatisticsDTOQueryWrapper(archiveIds);
        queryWrapper.between("freeze_time", beginAndEndDate.get("beginDate"), beginAndEndDate.get("endDate"));

        List<JumpAndAnticlimaxChartDTO> jumpAndAnticlimaxChartDTOList = reportHistorydataPOMapper.selectJumpAndAnticlimaxChart(
                queryWrapper, differenceType, differenceValue);

        List<JumpAndAnticlimaxChartVO> jumpAndAnticlimaxPageVOS = convertToJumpAndAnticlimaxChartVO(jumpAndAnticlimaxChartDTOList);
        return jumpAndAnticlimaxPageVOS;
    }

    /**
     * 突增突减图表DTO转vo
     * @param jumpAndAnticlimaxChartDTOList
     * @return
     */
    private List<JumpAndAnticlimaxChartVO> convertToJumpAndAnticlimaxChartVO(List<JumpAndAnticlimaxChartDTO> jumpAndAnticlimaxChartDTOList) {
        List<JumpAndAnticlimaxChartVO> res = new ArrayList<>(jumpAndAnticlimaxChartDTOList.size());
        JumpAndAnticlimaxChartVO jumpAndAnticlimaxChartVO;
        for (JumpAndAnticlimaxChartDTO jumpAndAnticlimaxChartDTO : jumpAndAnticlimaxChartDTOList) {
            jumpAndAnticlimaxChartVO = new JumpAndAnticlimaxChartVO();
            jumpAndAnticlimaxChartVO.setValue(jumpAndAnticlimaxChartDTO.getSum().setScale(2,RoundingMode.HALF_UP).toPlainString());
            jumpAndAnticlimaxChartVO.setCustomerName(jumpAndAnticlimaxChartDTO.getCustomerName());
            res.add(jumpAndAnticlimaxChartVO);
        }
        return res;
    }

    /**
     * 突增突减DTO转VO
     *
     * @param records
     * @param cycle
     * @return
     */
    private List<JumpAndAnticlimaxPageVO> convertToJumpAndAnticlimaxPageVO(List<JumpAndAnticlimaxPageDTO> records, String cycle) {
        List<JumpAndAnticlimaxPageVO> res = new ArrayList<>(records.size());
        JumpAndAnticlimaxPageVO jumpAndAnticlimaxPageVO;
        long i = 1;
        for (JumpAndAnticlimaxPageDTO record : records) {
            jumpAndAnticlimaxPageVO = new JumpAndAnticlimaxPageVO();
            jumpAndAnticlimaxPageVO.setId(jumpAndAnticlimaxPageVO.getId());
            jumpAndAnticlimaxPageVO.setSerialNum(i);
            jumpAndAnticlimaxPageVO.setCustomerName(record.getCustomerName());
            jumpAndAnticlimaxPageVO.setAreaName(record.getAreaName());
            jumpAndAnticlimaxPageVO.setCustomerSerialnumber(record.getCustomerSerialnumber());
            jumpAndAnticlimaxPageVO.setSerialnumber(record.getSerialnumber());
            jumpAndAnticlimaxPageVO.setSum(record.getSum().setScale(2, RoundingMode.HALF_UP).toPlainString());
            jumpAndAnticlimaxPageVO.setStartReadingnum(record.getStartReadingnum().setScale(2, RoundingMode.HALF_UP).toPlainString());
            jumpAndAnticlimaxPageVO.setEndReadingnum(record.getEndReadingnum().setScale(2, RoundingMode.HALF_UP).toPlainString());
            jumpAndAnticlimaxPageVO.setCycle(SystemConsts.DATE_CYCLE_TYPE.getEnumByValue(cycle) == null ? null : SystemConsts.DATE_CYCLE_TYPE.getEnumByValue(cycle).valueName);
            res.add(jumpAndAnticlimaxPageVO);
            i++;
        }
        return res;
    }

    /**
     * 零用量dto转vo
     *
     * @param records
     * @param cycle
     * @param beginAndEndDate
     * @return
     */
    private List<ZeroDosagePageVO> convertToZeroDosagePageVO(List<ZeroDosagePageDTO> records, String cycle, Map<String, String> beginAndEndDate) {
        List<ZeroDosagePageVO> res = new ArrayList<>(records.size());
        ZeroDosagePageVO zeroDosagePageVO;
        long i = 1;
        for (ZeroDosagePageDTO record : records) {
            zeroDosagePageVO = new ZeroDosagePageVO();
            zeroDosagePageVO.setId(record.getId());
            zeroDosagePageVO.setSerialNum(i);
            zeroDosagePageVO.setAreaName(record.getAreaName());
            zeroDosagePageVO.setCustomerName(record.getCustomerName());
            zeroDosagePageVO.setCustomerSerialnumber(record.getCustomerSerialnumber());
            zeroDosagePageVO.setSerialnumber(record.getSerialnumber());
            zeroDosagePageVO.setDate(SystemConsts.DATE_CYCLE_TYPE.DAY.value.equals(cycle) ? beginAndEndDate.get("begin") : getDatePeriod(beginAndEndDate.get("begin"), beginAndEndDate.get("end")));
            res.add(zeroDosagePageVO);
            i++;
        }
        return res;
    }

    /**
     * 用户用量图表dto转vo
     *
     * @param userDosageChartDTOS
     * @return
     */
    private List<UserDosageChartVO> convertToUserDosageChartVO(List<UserDosageChartDTO> userDosageChartDTOS) {
        List<UserDosageChartVO> res = new ArrayList<>(userDosageChartDTOS.size());
        UserDosageChartVO userDosageChartVO;
        long i = 1;
        for (UserDosageChartDTO record : userDosageChartDTOS) {
            userDosageChartVO = new UserDosageChartVO();
            userDosageChartVO.setCustomerName(record.getCustomerName());
            userDosageChartVO.setStartReadingnum(record.getStartReadingnum().setScale(2, RoundingMode.HALF_UP).toPlainString());
            userDosageChartVO.setEndReadingnum(record.getEndReadingnum().setScale(2, RoundingMode.HALF_UP).toPlainString());
            res.add(userDosageChartVO);
            i++;
        }
        return res;
    }

    /**
     * 用户用量dto转vo
     *
     * @param records
     * @param cycle
     * @param beginAndEndDate
     * @return
     */
    private List<UserDosagePageVO> convertToUserDosagePageVO(List<UserDosagePageDTO> records, String cycle, Map<String, String> beginAndEndDate) {
        List<UserDosagePageVO> res = new ArrayList<>(records.size());
        UserDosagePageVO userDosagePageVO;
        long i = 1;
        for (UserDosagePageDTO record : records) {
            userDosagePageVO = new UserDosagePageVO();
            userDosagePageVO.setId(record.getId());
            userDosagePageVO.setSerialNum(i);
            userDosagePageVO.setAreaName(record.getAreaName());
            userDosagePageVO.setCustomerName(record.getCustomerName());
            userDosagePageVO.setCustomerSerialnumber(record.getCustomerSerialnumber());
            userDosagePageVO.setSerialnumber(record.getSerialnumber());
            userDosagePageVO.setDate(SystemConsts.DATE_CYCLE_TYPE.DAY.value.equals(cycle) ? beginAndEndDate.get("begin") : getDatePeriod(beginAndEndDate.get("begin"), beginAndEndDate.get("end")));
            userDosagePageVO.setStartReadingnum(record.getStartReadingnum().setScale(2, RoundingMode.HALF_UP).toPlainString());
            userDosagePageVO.setEndReadingnum(record.getEndReadingnum().setScale(2, RoundingMode.HALF_UP).toPlainString());
            userDosagePageVO.setSum(record.getSum().setScale(2, RoundingMode.HALF_UP).toPlainString());
            res.add(userDosagePageVO);
            i++;
        }
        return res;
    }

    /**
     * dto转成vo
     *
     * @param statisticsDTOList
     * @param datesBetweenTwoDate
     * @return
     */
    private List<ChartVO> convertToTrendChartVO(List<StatisticsDTO> statisticsDTOList, List<String> datesBetweenTwoDate) {
        List<ChartVO> res = new ArrayList<>(datesBetweenTwoDate.size());
        ChartVO chartVO;
        for (String date : datesBetweenTwoDate) {
            boolean hasValue = false;
            for (StatisticsDTO statisticsDTO : statisticsDTOList) {
                if (date.equals(statisticsDTO.getFreezeTime())) {
                    chartVO = new ChartVO();
                    chartVO.setDatetime(date);
                    chartVO.setValue(statisticsDTO.getDosage().setScale(2, RoundingMode.HALF_UP).toPlainString());
                    res.add(chartVO);
                    hasValue = true;
                    break;
                }
            }
            if (!hasValue) {
                chartVO = new ChartVO();
                chartVO.setDatetime(date);
                chartVO.setValue(SystemConsts.ZERO_2);
                res.add(chartVO);
            }
        }

        return res;
    }

    /**
     * 表格dto转vo
     *
     * @param statisticsDTO
     * @param beginAndEndDate
     * @return
     */
    private List<ChartVO> convertToStatisticsChartVO(StatisticsDTO statisticsDTO, Map<String, String> beginAndEndDate) {
        List<ChartVO> res = new ArrayList<>();
        ChartVO chartVO = new ChartVO();
        chartVO.setDatetime(getDatePeriod(beginAndEndDate.get("begin"), beginAndEndDate.get("end")));
        chartVO.setValue(statisticsDTO.getDosage() == null ? SystemConsts.ZERO_2 : statisticsDTO.getDosage().setScale(2, RoundingMode.HALF_UP).toPlainString());
        ChartVO lastYearChartVO = new ChartVO();
        lastYearChartVO.setDatetime(getDatePeriod(beginAndEndDate.get("lastBegin"), beginAndEndDate.get("lastEnd")));
        lastYearChartVO.setValue(statisticsDTO.getLastYearDosage() == null ? SystemConsts.ZERO_2 : statisticsDTO.getLastYearDosage().setScale(2, RoundingMode.HALF_UP).toPlainString());
        res.add(chartVO);
        res.add(lastYearChartVO);
        return res;
    }

    /**
     * 获取查询条件
     *
     * @param archiveIds
     * @return
     */
    private QueryWrapper<StatisticsDTO> getStatisticsDTOQueryWrapper(List<Long> archiveIds) {
        QueryWrapper<StatisticsDTO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del", SystemConsts.BooleanEnum.FALSE.value);
        EnergyTypeUtil.injectEnergyTypeWrapper(queryWrapper);
        queryWrapper.in("archives", archiveIds);
        return queryWrapper;
    }

    /**
     * dto转vo
     *
     * @param records
     * @param beginAndEndDate
     * @return
     */
    private List<StatisticsPageVO> convertToStatisticsPageVO(List<StatisticsDTO> records, Map<String, String> beginAndEndDate) {
        List<StatisticsPageVO> res = new ArrayList<>(records.size());
        StatisticsPageVO statisticsPageVO;
        long i = 1;
        for (StatisticsDTO record : records) {
            statisticsPageVO = new StatisticsPageVO();
            statisticsPageVO.setId(record.getId());
            statisticsPageVO.setSerialNum(i);
            statisticsPageVO.setAreaName(record.getAreaName());
            statisticsPageVO.setCustomerName(record.getCustomerName());
            statisticsPageVO.setCustomerSerialnumber(record.getCustomerSerialnumber());
            statisticsPageVO.setDosage(record.getDosage() == null ? SystemConsts.ZERO_2 : record.getDosage().setScale(2, BigDecimal.ROUND_UP).toPlainString());
            statisticsPageVO.setLastYearDosage(record.getLastYearDosage() == null ? SystemConsts.ZERO_2 : record.getLastYearDosage().setScale(2, BigDecimal.ROUND_UP).toPlainString());
            statisticsPageVO.setDate(getDatePeriod(beginAndEndDate.get("begin"), beginAndEndDate.get("end")));
            statisticsPageVO.setLastYearDate(getDatePeriod(beginAndEndDate.get("lastBegin"), beginAndEndDate.get("lastEnd")));
            res.add(statisticsPageVO);
            i++;
        }
        return res;
    }

    /**
     * 获取日期区间
     *
     * @param start
     * @param end
     * @return
     */
    private String getDatePeriod(String start, String end) {
        return start + "~" + end;
    }


}
